#include "instant.h"
int play(SDL_Surface *e)
{
SDL_Surface *Background,*Bruke[6],*Ennemie;
SDL_Rect Position,Position_Bruke,Position_Laser,Position_Ennemie;
SDL_Event eve;
int continuer=1,i=0,x,j;
SDL_Init(SDL_INIT_VIDEO);

e=SDL_SetVideoMode(1000,768,32,SDL_HWSURFACE);
Ennemie=SDL_LoadBMP("coin.bmp");

Position.x=0;
Position.y=0;

Position_Ennemie.x=500;
Position_Ennemie.y=508;

Position_Bruke.x=15;
Position_Bruke.y=508;



Bruke[0]=IMG_Load("1.jpg");
Bruke[1]=IMG_Load("2.jpg");
Bruke[2]=IMG_Load("3.jpg");
Bruke[3]=IMG_Load("4.jpg");
Bruke[4]=IMG_Load("5.jpg");
Bruke[5]=IMG_Load("6.jpg");
//Bruke[6]=SDL_LoadBMP("7.bmp");
//Bruke[7]=SDL_LoadBMP("8.bmp");
//laser=SDL_LoadBMP("boulefeu.bmp");


Background=SDL_LoadBMP("bk1.bmp");

SDL_EnableKeyRepeat(5,5);
//SDL_SetColorKey(laser,SDL_SRCCOLORKEY,SDL_MapRGB(laser->format,255,255,255));

SDL_SetColorKey(Bruke[0],SDL_SRCCOLORKEY,SDL_MapRGB(Bruke[0]->format,255,255,255));
SDL_SetColorKey(Bruke[1],SDL_SRCCOLORKEY,SDL_MapRGB(Bruke[1]->format,255,255,255));
SDL_SetColorKey(Bruke[2],SDL_SRCCOLORKEY,SDL_MapRGB(Bruke[2]->format,255,255,255));

SDL_SetColorKey(Bruke[3],SDL_SRCCOLORKEY,SDL_MapRGB(Bruke[0]->format,255,255,255));
SDL_SetColorKey(Bruke[4],SDL_SRCCOLORKEY,SDL_MapRGB(Bruke[1]->format,255,255,255));
SDL_SetColorKey(Bruke[5],SDL_SRCCOLORKEY,SDL_MapRGB(Bruke[2]->format,255,255,255));

SDL_SetColorKey(Ennemie,SDL_SRCCOLORKEY,SDL_MapRGB(Ennemie->format,255,255,255));

SDL_FillRect(e,NULL,SDL_MapRGB(e->format,0,0,0));
SDL_BlitSurface(Background,NULL,e,&Position);
SDL_BlitSurface(Ennemie,NULL,e,&Position_Ennemie);
SDL_BlitSurface(Bruke[i],NULL,e,&Position_Bruke);
SDL_Flip(e);	
	while(continuer==1)
	{
	SDL_WaitEvent(&eve);
			
		switch(eve.key.keysym.sym)		
		{	
		case SDLK_RIGHT://walking RIGHT dude
			if(Position_Bruke.x+5==Position_Ennemie.x-100)//||(Position_Bruke.x+5==Position_Ennemie.x-Ennemie->w))//scrolling
			break;			
			else if((Position_Bruke.x>=e->w/2)&&(Position.x<11000))//scrolling		
			{	
			Position.x=Position.x+5;
			i++;
			}
						
			else//Deplacement normal
			{
			Position_Bruke.x=Position_Bruke.x+5;			
			i++;
			}
			if(i<3)
			i++;
			else if(i>2)
			i=0;			
		break;
				
		case SDLK_LEFT://walking left dude 
			if((Position_Bruke.x>=e->w/2)&&(Position.x>2000))		
			{	
			Position.x=Position.x-3;
			i++;		
			}
			else
			{
			Position_Bruke.x=Position_Bruke.x-5;			
			i++;
			}	
			if(i<5)
			i++;
			else if(i>5)
			i=3;	
			break;
		
		case SDLK_a:
				
				if(i<=2)	
				{
					for(x=0;x<20;x++)
					{
					/*SDL_FillRect(e,NULL,SDL_MapRGB(e->format,0,0,0));
					SDL_BlitSurface(Background,&Position,e,NULL);	
	        			SDL_BlitSurface(laser,NULL,e,&Position_Laser);		
					SDL_BlitSurface(Bruke[i],NULL,e,&Position_Bruke);	
					SDL_BlitSurface(Ennemie,NULL,e,&Position_Ennemie);					
					SDL_Flip(e);	
					SDL_Delay(25);*/		
					Position_Laser.x=Position_Laser.x+15;
					if(Position_Laser.x>=Position_Ennemie.x)
						Ennemie=NULL;					
					}				
				}
				else if(i>=3) 
				{Position_Laser.x=Position_Bruke.x-Bruke[0]->w+2;
					for(x=0;x<20;x++)
					{
				SDL_FillRect(e,NULL,SDL_MapRGB(e->format,0,0,0));
				SDL_BlitSurface(Background,&Position,e,NULL);	
	        		//SDL_BlitSurface(laser,NULL,e,&Position_Laser);		
				SDL_BlitSurface(Ennemie,NULL,e,&Position_Ennemie);
				SDL_BlitSurface(Bruke[i],NULL,e,&Position_Bruke);	
				SDL_Flip(e);	
				//SDL_Delay(25);		
				Position_Laser.x=Position_Laser.x-15;
					if(Position_Laser.x<=Position_Ennemie.x)
						Ennemie=NULL;					
					}
				}			
						
			Position_Laser.x=Position_Bruke.x+Bruke[0]->w+2;
		break;		
			case SDLK_SPACE:
				for(j=0;j<52;j++);			
				{
				//SDL_Delay(100);				
				Position_Bruke.x=Position_Bruke.x+100;
				Position_Bruke.y=Position_Bruke.y-100;
				SDL_FillRect(e,NULL,SDL_MapRGB(e->format,0,0,0));
				SDL_BlitSurface(Background,&Position,e,NULL);	
				SDL_BlitSurface(Bruke[i],NULL,e,&Position_Bruke);	
				SDL_BlitSurface(Ennemie,NULL,e,&Position_Ennemie);
				SDL_Flip(e);		
				}			
		SDL_Delay(20);		
		Position_Bruke.y=490;		
		break;
		case SDLK_ESCAPE://Exit part 
		continuer=0;
		break;
		} 	
	if(Position_Bruke.x>Position_Ennemie.x+150)
	Ennemie=NULL;



	SDL_FillRect(e,NULL,SDL_MapRGB(e->format,0,0,0));
	SDL_BlitSurface(Background,&Position,e,NULL);	
	SDL_BlitSurface(Bruke[i],NULL,e,&Position_Bruke);	
	SDL_BlitSurface(Ennemie,NULL,e,&Position_Ennemie);
	SDL_Flip(e);			
	SDL_Delay(10);
	}
return EXIT_SUCCESS;

}
